﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Vara_Data_Layer.Vara_Data_Asset;
using Vara_Data_Layer.Vara_Data_Make_Model;

namespace AssetTrackingModals.User_Controls.Asset_Module.Tabs
{
    public partial class Tab_1_Details : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        public Vara_Data_IAsset Save_Interface(Vara_Data_IAsset AssetInformationToSave)
        {
            AssetInformationToSave.AssetLabel = TextBox_Serial_Number.Text;
            AssetInformationToSave.SerialNumber = TextBox_Serial_Number.Text;
            AssetInformationToSave.DisplayName = TextBox_Display_Name.Text;

            if (DropDownList_Make.SelectedItem.Value != null && DropDownList_Make.SelectedItem.Value != "-1")
            {
                Vara_Data_Make_Controller MakeController = new Vara_Data_Make_Controller();
                Vara_Data_IMake MakeToReturn = MakeController.ReturnMake(int.Parse(DropDownList_Make.SelectedItem.Value));
                AssetInformationToSave.Make = MakeToReturn;
            }
            if (DropDownList_Model.SelectedItem.Value != null && DropDownList_Model.SelectedItem.Value != "-1")
            {
                Vara_Data_Model_Controller ModelController = new Vara_Data_Model_Controller();
                Vara_Data_IModel ModelToReturn = ModelController.ReturnModel(int.Parse(DropDownList_Model.SelectedItem.Value));
                AssetInformationToSave.Model = ModelToReturn;
            }

            return AssetInformationToSave;
        }
        public void Load_Interface(Vara_Data_IAsset AssetInformationToLoad)
        {
            //set the textbox values to the asset values 
            LabelAssetIdentifier.Text = AssetInformationToLoad.ID.ToString();
            TextBox_AssetLabel.Text = AssetInformationToLoad.AssetLabel;
            TextBox_Serial_Number.Text = AssetInformationToLoad.SerialNumber;
            TextBox_Display_Name.Text = AssetInformationToLoad.DisplayName;
            //populate the makes dropdown
            PopulateDropDownListMakes();
            //if the asset has a make listed them select it
            if (AssetInformationToLoad.Make != null)
            {
                if (DropDownList_Make.Items.FindByValue(AssetInformationToLoad.Make.ID.ToString()) != null)
                {
                    DropDownList_Make.Items.FindByValue(AssetInformationToLoad.Make.ID.ToString()).Selected = true;
                    DropDownList_Make_SelectedIndexChanged("", EventArgs.Empty);
                    if (DropDownList_Model.Items.FindByValue(AssetInformationToLoad.Model.ID.ToString()) != null)
                    {
                        DropDownList_Model.Items.FindByValue(AssetInformationToLoad.Model.ID.ToString()).Selected = true;
                    }
                }
            }
        }
        //Return all of the makes and populate the drop down list with them
        protected void PopulateDropDownListMakes()
        {
            DropDownList_Make.DataSource = new Vara_Data_Make_Controller().Makes;
            DropDownList_Make.DataTextField = "Name";
            DropDownList_Make.DataValueField = "ID";
            DropDownList_Make.DataBind();
        }
        //When a make has been selected then return the attached models
        protected void DropDownList_Make_SelectedIndexChanged(object sender, EventArgs e)
        {
            DropDownList_Model.DataSource = new Vara_Data_Make_Controller().ReturnMake(int.Parse(DropDownList_Make.SelectedItem.Value)).Models;
            DropDownList_Model.DataTextField = "Name";
            DropDownList_Model.DataValueField = "ID";
            DropDownList_Model.DataBind();
        }
        //Returns the asset currently loaded 
        public Vara_Data_IAsset ReturnAssetCurrentlyLoaded()
        {
            string assetID = LabelAssetIdentifier.Text.ToString();
            Vara_Data_Asset_Controller AssetController = new Vara_Data_Asset_Controller();
            Vara_Data_IAsset AssetToReturn = AssetController.ReturnAsset(int.Parse(assetID));
            return AssetToReturn;
        }
    }
}